var Select2 = function() {


    var selectMember = function() {
        var content = $('.members_url').val();

        // 选择人员
        $('.select-member').on('click', function(){
            var index = layer.open({
                title : "选择人员",
                type : 2,
                area : ["900px", "600px"],
                content : content,
                success : function(layero, index){
                    //
                }
            });
        });
    };

    var selectHouseholder = function() {
        var householder_member_url = $('.householder_member_url').val();

        // 选择户主
        $('.select-householder').on('click', function(){
            var townId = $('.town').val();
            var villageId = $('.village').val();
            if ($('.member-type').length > 0) {
                var member_type = $('.member-type').val();
                if(member_type == 1){
                    if(villageId == 0 && townId == 0) {
                        layer.msg('请先选择乡村');
                        return;
                    }
                }
            } else {
                if(villageId == 0 && townId == 0) {
                    layer.msg('请先选择乡村');
                    return;
                }
            }
            var memberId = $('input[name=id]').val();
            var param = {
                'memberId':memberId
            };
            param.townId = townId;

            var title = $(this).attr('data-title');
            var content = '<div class="col-md-9"><div class="form-inline"><form id="searchForm" class="relative row"><input type="text" class="form-control" name="title" placeholder="姓名" maxlength="44"><button type="button" class="btn btn-success doQuery" title="筛选"> <i class="fa fa-search"></i> 筛选</button></form></div></div>'+
                '<table class="table" id="householder_table"><thead><tr><th> 村子 </th><th> 姓名 </th><th> 手机号 </th><th> 操作 </th></tr></thead></table>';
            var options = {
                'width' : '1000px',
            };
            open_modal(title, content, options);
            var table = $('#householder_table');

            table.dataTable({
                "processing": true, // 开启服务器模式
                "ordering": false, // 禁止排序
                serverSide: true,
                destroy:false,
                "ajax": {
                    url: householder_member_url,
                    data: param
                },
                "autoWidth": false,
                "columns": [
                    {"width": "20%","data": "village"},
                    {"width": "20%","data": "name"},
                    {"width": "20%","data": "mobile"},
                    {
                        "width": "20%",
                        "data": "null",
                        "render": function(data, type, row, meta){
                            return '<button type="button" class="btn btn-success select-btn">选取<input type="radio" style="display:none" name="memberId" value="'+row.id+'" /></button>';
                        }
                    }
                ],
                "language":{
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "pageLength": 7,
                "columnDefs": [{
                    "orderable": false,
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'table_b relative'<'col-md-5'i><'col-md-7'>p>",
                "fnCreatedRow": function(nRow, aData, iDataIndex){
                    if(aData['id'] == $('input[name="householderId"]').val()) {
                        $(nRow).find(".select-btn").attr('style','background:#5cb85c');
                        $(nRow).find(".select-btn").find('input').prop('checked',true);
                    }
                },
                "fnInitComplete": function() {}
            });

            // 选中
            table.on('click', '.select-btn', function(event) {
                // 操作行对象
                var dataArr = table.DataTable().rows($(this).parents("tr")).data();
                var id = dataArr[0].id;
                var selectTxt = dataArr[0].name;

                $("input[name='householderId']").val(id);
                $(".select-householder").html(selectTxt);
                $(this).parents(".modal").modal('hide');
            });


            // 按名称搜索
            $('.doQuery').on('click', function(event){
                var params = {'name':$("#searchForm input[name='title']").val()};
                $('#householder_table').DataTable().search(JSON.stringify(params)).draw();
            });

            $('.city, .xian, .town, .village').on('change', function(){
                $('input[name=householderId]').val('');
                $('.select-householder').empty().html('选择户主');
            });
        });
    };



    return {
        init: function() {
            selectMember();
            selectHouseholder();
        }

    };

}();

jQuery(document).ready(function() {
    Select2.init();

});